package com.netease.nim.uikit.business.session.actions;

import android.Manifest;
import android.support.v4.app.FragmentActivity;

import com.aiyige.R;
import com.aiyige.model.tag.Tag;
import com.aiyige.utils.ToastX;
import com.netease.nim.uikit.api.model.location.LocationProvider;
import com.netease.nim.uikit.impl.NimUIKitImpl;
import com.netease.nimlib.sdk.msg.MessageBuilder;
import com.netease.nimlib.sdk.msg.model.IMMessage;
import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.functions.Consumer;

/**
 * Created by hzxuwen on 2015/6/12.
 */
public class LocationAction extends BaseAction {
    private final static String TAG = "LocationAction";

    public LocationAction() {
        super(R.drawable.nim_message_plus_location_selector, R.string.input_panel_location);
    }

    @Override
    public void onClick() {
        RxPermissions rxPermissions = new RxPermissions((FragmentActivity) getActivity());
        rxPermissions.request(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if (aBoolean) {
                            if (NimUIKitImpl.getLocationProvider() != null) {
                                NimUIKitImpl.getLocationProvider().requestLocation(getActivity(), new LocationProvider.Callback() {
                                    @Override
                                    public void onSuccess(double longitude, double latitude, String address) {
                                        IMMessage message = MessageBuilder.createLocationMessage(getAccount(), getSessionType(), latitude, longitude,
                                                address);
                                        sendMessage(message);
                                    }
                                });
                            }
                        } else {
                            ToastX.show(R.string.please_grant_location_permission);
                        }
                    }
                });
    }
}
